package com.quanyan.financialSystem.service.place.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.financialSystem.entity.db.TbBizOrderMemberReconciliation;
import com.quanyan.financialSystem.entity.db.TbBizOrderMemberReconciliationExample;
import com.quanyan.financialSystem.entity.vo.place.req.ReqOrderReconciliation;
import com.quanyan.financialSystem.entity.vo.place.req.ReqOrderReconciliationUpdate;
import com.quanyan.financialSystem.entity.vo.place.resp.OrderReconciliationVo;
import com.quanyan.financialSystem.entity.vo.place.resp.OrderReconciliationVos;
import com.quanyan.financialSystem.mapper.OrderReconciliationMapper;
import com.quanyan.financialSystem.mapper.TbBizOrderMemberReconciliationMapper;
import com.quanyan.financialSystem.service.place.OrderReconciliationService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * OrderReconciliationServiceImpl
 *
 * @author Tu Chun
 * @date 2016/10/24
 */
@Service("orderReconciliationService")
public class OrderReconciliationServiceImpl implements OrderReconciliationService {
    @Autowired
    OrderReconciliationMapper orderReconciliationMapper;
    @Autowired
    TbBizOrderMemberReconciliationMapper tbBizOrderMemberReconciliationMapper;

    @Override
    public APIResponse<PageObj<OrderReconciliationVos>> getOrderReconciliation(ReqOrderReconciliation reqOrderReconciliation) {
        Integer pageNum = reqOrderReconciliation.getPageNum();
        Integer pageSize = reqOrderReconciliation.getPageSize();
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 30;
        }
        reqOrderReconciliation.setPageStart((pageNum - 1) * pageSize);
        reqOrderReconciliation.setPageNum(pageNum);
        reqOrderReconciliation.setPageSize(pageSize);
        OrderReconciliationVos orderReconciliationVoss = new OrderReconciliationVos();
        String name = reqOrderReconciliation.getName();
        String serialNo = reqOrderReconciliation.getSerialNo();
        String placeOrderNo = reqOrderReconciliation.getPlaceOrderNo();
        if (name != null)
            reqOrderReconciliation.setName(name.trim());
        if (serialNo != null)
            reqOrderReconciliation.setSerialNo(serialNo.trim());
        if (placeOrderNo != null)
            reqOrderReconciliation.setPlaceOrderNo(placeOrderNo.trim());
        List<OrderReconciliationVo> orderReconciliationVos = orderReconciliationMapper.getOrderReconciliation(reqOrderReconciliation);
        //计算非退款总额
        reqOrderReconciliation.setType((byte)1);
        Map<String,Object> map1=orderReconciliationMapper.getOrderReconciliationDifferenceAmount(reqOrderReconciliation);
        Double actualTotalAmount1=MapUtils.getDouble(map1, "actualTotalAmount")==null?0:MapUtils.getDouble(map1, "actualTotalAmount");
        Double arrivalTotalAmount1=MapUtils.getDouble(map1, "arrivalTotalAmount")==null?0:MapUtils.getDouble(map1, "arrivalTotalAmount");
        //计算退款总额
        reqOrderReconciliation.setType((byte)2);
        Map<String,Object> map2=orderReconciliationMapper.getOrderReconciliationDifferenceAmount(reqOrderReconciliation);
        Double actualTotalAmount2=MapUtils.getDouble(map2, "actualTotalAmount")==null?0:MapUtils.getDouble(map2, "actualTotalAmount");
        Double arrivalTotalAmount2=MapUtils.getDouble(map2, "arrivalTotalAmount")==null?0:MapUtils.getDouble(map2, "arrivalTotalAmount");

        orderReconciliationVoss.setActualTotalAmount(new BigDecimal(actualTotalAmount1).subtract(new BigDecimal(actualTotalAmount2)));
        orderReconciliationVoss.setArrivalTotalAmount(new BigDecimal(arrivalTotalAmount1).subtract(new BigDecimal(arrivalTotalAmount2)));
        orderReconciliationVoss.setDifferenceTotalAmount(orderReconciliationVoss.getArrivalTotalAmount().subtract(orderReconciliationVoss.getActualTotalAmount()).setScale(2, RoundingMode.HALF_UP));
        orderReconciliationVoss.setOrderReconciliationVos(orderReconciliationVos);
        Integer count = orderReconciliationMapper.orderReconciliationCount(reqOrderReconciliation);
        PageObj<OrderReconciliationVos> result = PageObj.create(count, pageNum, pageSize, orderReconciliationVoss);
        return APIResponse.returnSuccess(result);
    }

    @Override
    public APIResponse<OrderReconciliationVos> orderReconciliationAll(ReqOrderReconciliation reqOrderReconciliation) {
        OrderReconciliationVos orderReconciliationVoss = new OrderReconciliationVos();
        List<OrderReconciliationVo> orderReconciliationVos = orderReconciliationMapper.getOrderReconciliationAll(reqOrderReconciliation);
        Map<String,Object> map=orderReconciliationMapper.getOrderReconciliationDifferenceAmount(reqOrderReconciliation);
        //计算非退款总额
        reqOrderReconciliation.setType((byte)1);
        Map<String,Object> map1=orderReconciliationMapper.getOrderReconciliationDifferenceAmount(reqOrderReconciliation);
        Double actualTotalAmount1=MapUtils.getDouble(map1, "actualTotalAmount")==null?0:MapUtils.getDouble(map1, "actualTotalAmount");
        Double arrivalTotalAmount1=MapUtils.getDouble(map1, "arrivalTotalAmount")==null?0:MapUtils.getDouble(map1, "arrivalTotalAmount");
        //计算退款总额
        reqOrderReconciliation.setType((byte)2);
        Map<String,Object> map2=orderReconciliationMapper.getOrderReconciliationDifferenceAmount(reqOrderReconciliation);
        Double actualTotalAmount2=MapUtils.getDouble(map2, "actualTotalAmount")==null?0:MapUtils.getDouble(map2, "actualTotalAmount");
        Double arrivalTotalAmount2=MapUtils.getDouble(map2, "arrivalTotalAmount")==null?0:MapUtils.getDouble(map2, "arrivalTotalAmount");

        orderReconciliationVoss.setActualTotalAmount(new BigDecimal(actualTotalAmount1).subtract(new BigDecimal(actualTotalAmount2)));
        orderReconciliationVoss.setArrivalTotalAmount(new BigDecimal(arrivalTotalAmount1).subtract(new BigDecimal(arrivalTotalAmount2)));
        orderReconciliationVoss.setDifferenceTotalAmount(orderReconciliationVoss.getActualTotalAmount().subtract(orderReconciliationVoss.getArrivalTotalAmount()));
        orderReconciliationVoss.setOrderReconciliationVos(orderReconciliationVos);
        return APIResponse.returnSuccess(orderReconciliationVoss);
    }

    @Override
    public APIResponse<?> updateOrderStatus(ReqOrderReconciliationUpdate reqOrderReconciliationUpdate) {
        TbBizOrderMemberReconciliation tbBizOrderMemberReconciliation = new TbBizOrderMemberReconciliation();
        tbBizOrderMemberReconciliation.setOpUid(reqOrderReconciliationUpdate.getUserId());
        tbBizOrderMemberReconciliation.setOpUserName(reqOrderReconciliationUpdate.getUserName());
        tbBizOrderMemberReconciliation.setReconciliationDate(new Date());
        tbBizOrderMemberReconciliation.setRemark(reqOrderReconciliationUpdate.getRemark());
        tbBizOrderMemberReconciliation.setRemark(reqOrderReconciliationUpdate.getRemark());
        tbBizOrderMemberReconciliation.setOpStatus((byte)1);
        tbBizOrderMemberReconciliation.setIsDifference((byte)1);
        TbBizOrderMemberReconciliationExample tbBizOrderMemberReconciliationExample = new TbBizOrderMemberReconciliationExample();
        tbBizOrderMemberReconciliationExample.createCriteria().andPlaceOrderIdEqualTo(reqOrderReconciliationUpdate.getPlaceOrderId()).andTypeEqualTo(reqOrderReconciliationUpdate.getType());
        int i = tbBizOrderMemberReconciliationMapper.updateByExampleSelective(tbBizOrderMemberReconciliation, tbBizOrderMemberReconciliationExample);
        if (i > 0) {
            return APIResponse.returnSuccess("跟新成功!");
        }
        return APIResponse.returnFail("跟新失败");
    }
}
